package com.example.isoftservice.controller;

import com.example.isoftservice.pojos.Address;
import com.example.isoftservice.pojos.DealerType;
import com.example.isoftservice.service.DealerTypeService;
import com.example.isoftservice.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/dealerType")
public class DealerTypeController {
    @Resource
    private DealerTypeService dealerTypeService;

    @RequestMapping("/searchList")
    public Result<Map<String,Object>> searchList(@RequestParam(name = "pageNo" ,defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize" ,defaultValue = "5")Integer pageSize) {
        Result<Map<String, Object>> result = new Result<>();
        result.setMessage("查询成功");
        result.setResult(dealerTypeService.searchList(pageNo,pageSize));
        result.setCode(0);
        return  result;
    }

    @RequestMapping("/searchById")
    public Result<DealerType> searchById(Integer dealerTypeId) {
        Result<DealerType> result = new Result<>();
        DealerType dealerType = dealerTypeService.searchById(dealerTypeId);
        if(dealerType != null) {
            result.setMessage("查询成功");
            result.setCode(0);
            result.setResult(dealerType);
            return result;
        }else {
            result.setMessage("查询失败,类别不存在");
            result.setCode(1);
            return result;
        }

    }

    @RequestMapping(value = "/addInfo" ,method = RequestMethod.POST)
    public Result<Boolean> addInfo(@RequestBody DealerType dealerType) {
        Result<Boolean> result = new Result<>();
        Boolean flag = dealerTypeService.addInfo(dealerType);
        result.setResult(flag);
        result.setCode(0);
        result.setMessage("增加成功");
        return result;
    }

    @RequestMapping(value = "/removeByIds", method = RequestMethod.POST)
    public Result<Boolean> removeByIds(@RequestBody DealerType dealerType) {
        Result<Boolean> result = new Result<>();
        Boolean flag = dealerTypeService.removeByIds(dealerType.getIds());
        result.setMessage("删除成功");
        result.setCode(0);
        result.setResult(flag);
        return result;
    }

    @RequestMapping(value = "/modifyById",method = RequestMethod.POST)
    public  Result<Boolean> modifyById(@RequestBody  DealerType dealerType) {
        Result<Boolean> result = new Result<>();
        Boolean flag = dealerTypeService.modifyById(dealerType);
        if(dealerTypeService.searchById(dealerType.getDealerTypeId()) != null) {
            result.setResult(flag);
            result.setCode(0);
            result.setMessage("更新成功");
            return result;
        } else {
            result.setCode(1);
            result.setMessage("更新失败,类别不存在");
            return result;
        }

    }
}
